//
//  AONetworkTools.swift
//  weibo_zha
//
//  Created by _zha on 16/10/25.
//  Copyright © 2016年 渣. All rights reserved.
//

import UIKit
import AFNetworking

enum AONetworkToolsMethod: String {
    case get = "get"
    case post = "post"
}

class AONetworkTools: AFHTTPSessionManager {
    
    static let sharedTools: AONetworkTools = {
        let tools = AONetworkTools()
        //反序列化
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    //网络请求方法
    func requet(method: AONetworkToolsMethod, urlString: String, parameters: Any?, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 如果是 get 请求
        if method == .get {
            get(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) in
                success(responseObject)
                }, failure: { (_, error) in
                    failure(error)
            })
        }else {
            // post 请求
            post(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) in
                success(responseObject)
                }, failure: { (_, error) in
                    failure(error)
            })
        }
    }
}

// MARK: - 发微博相关接口
extension AONetworkTools {
    //发送文字微博
    func update(access_token: String, status: String, success:@escaping (Any?)->(), failure:@escaping (Error)->()) {
        // 请求接口地址
        let url = "https://api.weibo.com/2/statuses/update.json"
        //请求参数
        let params : [String: Any] = [
            "access_token": access_token,
            "status": status
        ]
        // 执行网络请求
        requet(method: .post, urlString: url, parameters: params, success: success
            , failure: failure)
    }
}

// MARK: - 首页相关接口
extension AONetworkTools {
    func homeLoadData(sinceId: Int64, maxId: Int64,success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求 url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params: [String: Any] = [
            "access_token": AOUserAccountViewModel.sharedTools.accessToken!,
            "since_id" : sinceId,
            "max_id" : maxId
        ]
        // 发送请求
        requet(method: AONetworkToolsMethod.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}


//OAuth授权相关借口
extension AONetworkTools {
    // 获取 userAccount
    func oauthLoadUserAccount(code: String, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求 urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": AOAppKey,
            "client_secret": AOAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": AORedirectURI
        ]
        // 发送请求
        requet(method: AONetworkToolsMethod.post, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    // 请求用户信息
    func oauthLoadUserInfo(model: AOUserAccountModel, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求 urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token":model.access_token!,
            "uid":model.uid!
        ]
        // 发送请求
        requet(method: AONetworkToolsMethod.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
}

